package com.cast.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.cast.constant.MessageConstant;
import com.cast.entity.PageResult;
import com.cast.entity.QueryPageBean;
import com.cast.entity.Result;
import com.cast.pojo.CheckItem;
import com.cast.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage ==null || currentPage ==0){
            currentPage =1;
            queryPageBean.setCurrentPage(currentPage);
        }

        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null || pageSize ==0){
            pageSize =10;
            queryPageBean.setPageSize(pageSize);
        }

        PageResult pageResult = checkItemService.findByPage(queryPageBean);

        return pageResult;
    }

    @RequestMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.addCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result addCheckItem(int id){
        CheckItem item = checkItemService.findById(id);
        return new Result(true,null,item);
    }

    @RequestMapping("/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        try {
                checkItemService.updateCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/deleteCheckItem")
    public Result deleteById(int id){
        try {
            checkItemService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
}
